package com.lzx.musiclib;

import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lzx.musiclibrary.aidl.model.SongInfo;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mCyle;
    private Context mContext;
    // private ArrayList<Song> mList = new ArrayList<>();
    private ArrayList<SongInfo> mList = new ArrayList<>();
    private MediaPlayer mPlayer = new MediaPlayer();
    private Song mSong;
    private SongInfo mSongInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCyle = findViewById(R.id.recycle);
        mContext = this;
        //init();
    }

    private void init() {

        MusicAdapter adapter = new MusicAdapter();
        mCyle.setLayoutManager(new LinearLayoutManager(mContext));
        mCyle.setAdapter(adapter);
        adapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position, String path) {
                Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_SHORT).show();
           /*     MusicManager.get().playMusicByInfo(mSongInfo);
                //MusicManager.get().playMusicByIndex(position);
                MusicManager.get().reset();
                MusicManager.get().playMusic(mList,position);
                MusicManager.get().playPre();*/
                mPlayer.reset();
                try {
                    mPlayer.setDataSource(path);
                    mPlayer.prepareAsync();
                    mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mPlayer.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public void start(View view) {//停止
         mPlayer.stop();
       // MusicManager.get().stopMusic();
       // mPlayer.release();
    }

    public void pause(View view) {//暂停

            mPlayer.pause();
        //MusicManager.get().pauseMusic();

    }

    public void restart(View view) {
        /*if (mPlayer.r){
            MusicManager.get().resumeMusic();
        }*/
        mPlayer.start();
    }
    public void scanner(View view) {//扫描

        Cursor cursor = mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                mSongInfo = new SongInfo();
                mSongInfo.setSongName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                mSongInfo.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                mSongInfo.setSongUrl(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                mSongInfo.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
                mSongInfo.setSize(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
                mSongInfo.setSongId(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));


                if (mList != null) {
                    if (!mList.contains(mSongInfo)) {
                        mList.add(mSongInfo);
                    }
                } else {
                    mList.add(mSongInfo);
                }
            }
        }
        cursor.close();
        init();
    }


    private class MusicAdapter extends RecyclerView.Adapter {
        private ItemClickListener mItemClickListener;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_music, parent, false);

            return new MusicHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final MusicHolder musicHolder = (MusicHolder) holder;
            musicHolder.setData(position, mList);
            musicHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClickListener(musicHolder.itemView, position, mList.get(position).getSongUrl());
                }
            });

        }

        public void setOnItemClickListener(ItemClickListener itemClickListener) {

            mItemClickListener = itemClickListener;
        }

        @Override
        public int getItemCount() {
            return mList.size() > 0 ? mList.size() : 0;
        }

        private class MusicHolder extends RecyclerView.ViewHolder {

            private final TextView mTime;
            private final TextView title;
            private final TextView mNum;
            private final TextView id;
            private final TextView art;

            public MusicHolder(View view) {
                super(view);
                mNum = view.findViewById(R.id.num);
                title = view.findViewById(R.id.name);
                mTime = view.findViewById(R.id.time);
                art = view.findViewById(R.id.art);
                id = view.findViewById(R.id.id);
            }

            public void setData(int position, ArrayList<SongInfo> songs) {
                int time = (int) songs.get(position).getDuration();
                if (time / 1000 % 60 < 10) {
                    String tt = time / 1000 / 60 + ":0" + time / 1000 % 60;
                    mTime.setText(tt);
                } else {
                    String tt = time / 1000 / 60 + ":" + time / 1000 % 60;
                    mTime.setText(tt);
                }
                mNum.setText(position + "");
                title.setText(songs.get(position).getSongName());
                art.setText(songs.get(position).getArtist());
                // id.setText("ID:"+songs.get(position).getSongId() + "");
            }
        }
    }
}
